var BodyHeight;
var session = sessionStorage.getItem("session");
//document.body.scrollHeight=620;
if (!session) {
	window.parent.location = "../../login.html";
}
$(document).ready(function() {
	BodyHeight = window.screen.height;
	$("#UpdateAreaInfoGrid").jqGrid({
		url: IP + "GetAreaInfoList?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '县市名称',
			name: 'text',
			width: 200
		}],
		loadonce: true,
		viewrecords: true,
		height: 420,
		autowidth: true,
		//		width:400,
		rowNum: 18,
		page: 1,
		loadtext: '正在加载数据',
		pager: "#UpdateAreaInfoGridPager"
	});

	$(window).bind('resize', function() {
		$("#UpdateAreaInfoGrid").setGridWidth($(window).width());
		$("#UpdateAreaInfoGrid").setGridHeight($(window).height()-150);
	});
	document.onkeydown = function() {
		if (event.ctrlKey && event.keyCode == 13) {
			SaveRow();
		}
	}
	var template1 = {
		"groupOp": "AND",
		"rules": [{
			"field": "text",
			"op": "eq",
			"data": "W"
		}]
	};

	$('#UpdateAreaInfoGrid').navGrid("#UpdateAreaInfoGridPager", {
			search: true, // show search button on the toolbar
			add: false,
			edit: false,
			del: false,
			refresh: true
		}, {}, // edit options
		{}, // add options
		{}, // delete options
		{
			multipleSearch: true,
			multipleGroup: true,
			showQuery: true,
			// set the names of the template
			tmplNames: ["模板一"],
			// set the template contents
			tmplFilters: [template1]
		} // search options - define multiple search
	);
	AddRow();
});


function selectRow() {
	jQuery('#UpdateAreaInfoGrid').jqGrid('setSelection', '2');
}

function AddRow() {
	$('#AreaFf').form('load', {
		id: 0,
		AreaName: '',
	});
	$('#UpdateAreaInfoGrid').jqGrid('setSelection', id);
}

function EditRow() {
	var grid = $("#UpdateAreaInfoGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	if (rowKey) {
		$('#AreaFf').form('load', {
			id: $("#UpdateAreaInfoGrid").getCell(rowKey, "id"),
			AreaName: $("#UpdateAreaInfoGrid").getCell(rowKey, "text"),
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function DeleteRow() {
	var grid = $("#UpdateAreaInfoGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#UpdateAreaInfoGrid").getCell(rowKey, "text");
	if (rowKey) {
		$.messager.confirm('删除提醒', '你确认要删除改县市信息？---' + $("#UpdateAreaInfoGrid").getCell(rowKey, "text"), function(r) {
			if (r) {
				$.ajax({
					type: "post", // 与可以 "get"
					url: IP + "DeleteAreaInfo?callback=?",
					data: {
						id: rowKey,
						session: session,
					},
					dataType: "jsonp",
					jsonp: "callback",
					jsonpCallback: "resultHandler",
					success: function(json) {
						if (json.result == 200) {
							$("#UpdateAreaInfoGrid").delRowData(rowKey);
							//							insertopertlog(fmodel, foperand, faction, foldValue, fnewValue)
							insertopertlog('系统管理', '县市管理', '删除', oldvalue, '');
							$.messager.show({
								title: '提醒',
								msg: '删除成功',
								timeout: 1300,
								showType: 'slide',
								style: {
									right: '',
									bottom: ''
								}
							});
						} else {
							$.messager.alert('删除失败', json.message, 'error');
						}
					}
				});
			}
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function SaveRow() {
	console.log(loginName);
	var grid = $("#UpdateAreaInfoGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#UpdateAreaInfoGrid").getCell(rowKey, "text");
	if ($('#AreaFf').form('enableValidation').form('validate')) {

		$.ajax({
			type: "post", // 与可以 "get"
			url: IP + "UpdateAreaInfo?callback=?&session" + session,
			data: {
				id: $('#id').val(),
				areaName: $("#AreaName").val(),
			},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			success: function(json) {
				if (json.result = 200) {
					if ($('#id').val() == 0) { //如果ID传入不为零 用户在修改数据
						$("#UpdateAreaInfoGrid").addRowData(json.id, { //为零时，用户在增加数据
							id: json.id,
							text: $("#AreaName").val(),
						}, '');

						insertopertlog('系统管理', '县市管理', '新增', '', $("#AreaName").val());
						$.messager.show({
							title: '提醒',
							msg: '新增县市信息成功！',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					} else {
						$("#UpdateAreaInfoGrid").setRowData($('#id').val(), {
							id: $('#id').val(),
							text: $("#AreaName").val(),
						}, '');
						insertopertlog('系统管理', '县市管理', '修改', oldvalue, $("#AreaName").val());
						$.messager.show({
							title: '提醒',
							msg: '修改成功！',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					}
				} else {
					$.messager.alert('保存失败', json.message, 'error');
				}
				//保存成功后，重置控件，以免用户重复提交
				AddRow();
			}
		});
	}
}